<?php
/*
 * Copyright (C) 2004-2011 wortzwei GmbH <info@wortzwei.de>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @since 14.01.2011
 * @author Dirk Dittmar
 */

require_once( 'core.php' );
require_once( 'compress_api.php' );
require_once( dirname(dirname(__FILE__)) . '/inc/SolrSearch_SolrService.class.php' );

auth_reauthenticate( );
access_ensure_global_level( config_get( 'manage_site_threshold' ) );

$solrException = null;
if (isset($_POST['index'])) {
	try {
		$solr = new SolrSearch_SolrService(
					plugin_config_get(SolrSearchPlugin::SOLR_HOST),
					plugin_config_get(SolrSearchPlugin::SOLR_PORT),
					plugin_config_get(SolrSearchPlugin::SOLR_PATH)
				);
		$bugsAdded = $solr->rebuildIndex();
	} catch (Exception $e) {
		$solrException = $e;
	}
}

if (isset($_POST['save'])) {
	plugin_config_set(SolrSearchPlugin::SOLR_HOST, $_POST['host']);
	plugin_config_set(SolrSearchPlugin::SOLR_PORT, $_POST['port']);
	plugin_config_set(SolrSearchPlugin::SOLR_PATH, $_POST['path']);
	plugin_config_set(SolrSearchPlugin::SOLR_SCORE, isset($_POST['score']));
}

html_page_top(plugin_lang_get('title'));

$t_this_page = plugin_page('import');
print_manage_menu( $t_this_page );

?>
<div align="center">
<form method="POST" action="<?php echo plugin_page( 'config' ) ?>">
<table class="width50" cellspacing="1">
<tr>
	<td class="form-title">
		<?php echo plugin_lang_get('title') ?>
	</td>
	<td class="right" />
</tr>
<tr class="row-1">
	<td class="category" width="25%">
		<span class="required">*</span><?php echo plugin_lang_get('host') ?>
	</td>
	<td width="75%">
		<input type="text" name="host" size="32" value="<?php echo plugin_config_get(SolrSearchPlugin::SOLR_HOST) ?>" />
	</td>
</tr>
<tr class="row-2">
	<td class="category" width="25%">
		<span class="required">*</span><?php echo plugin_lang_get('port') ?>
	</td>
	<td width="75%">
		<input type="text" name="port" size="32" value="<?php echo plugin_config_get(SolrSearchPlugin::SOLR_PORT) ?>" />
	</td>
</tr>
<tr class="row-1">
	<td class="category" width="25%">
		<span class="required">*</span><?php echo plugin_lang_get('path') ?>
	</td>
	<td width="75%">
		<input type="text" name="path" size="32" value="<?php echo plugin_config_get(SolrSearchPlugin::SOLR_PATH) ?>" />
	</td>
</tr>
<tr class="row-2">
	<td class="category" width="25%">
		<span class="required">*</span><?php echo plugin_lang_get('show_score') ?>
	</td>
	<td width="75%">
		<input type="checkbox" name="score"
			<?php if (plugin_config_get(SolrSearchPlugin::SOLR_SCORE)) : ?>checked="checked"<?php endif; ?> />
	</td>
</tr>
<tr>
	<td class="left">
		<span class="required"> * <?php echo lang_get( 'required' ) ?></span>
	</td>
	<td class="center">
		<input type="submit" name="save" value="<?php echo plugin_lang_get( 'save_config_button' ) ?>" />
		<input type="submit" name="index" value="<?php echo plugin_lang_get('rebuild_index') ?>" />
	</td>
</tr>
<?php if (isset($solrException)) : ?>
<tr>
	<td class="left"></td>
	<td class="center">
		<font color="red"><?php echo $solrException->getMessage(); ?></font>
	</td>
</tr>
<?php elseif (isset($bugsAdded)) : ?>
<tr>
	<td class="left"></td>
	<td class="center">
		<?php
			echo plugin_lang_get('bugs_added');
			echo $bugsAdded;
		?>
	</td>
</tr>
<?php endif; ?>
</table>
</form>
</div>
<?php
html_page_bottom();
